package com.fulu.game.bi.service.impl.product;

import com.fulu.game.bi.entity.bo.channel.DictItemBO;
import com.fulu.game.bi.entity.bo.channel.NameAttrBO;
import com.fulu.game.bi.entity.po.DictItem;
import com.fulu.game.bi.entity.vo.common.CodeNameVO;
import com.fulu.game.bi.entity.vo.product.EventConditionVO;
import com.fulu.game.bi.hbase.BehaviourEventDetailStatDao;
import com.fulu.game.bi.mysql.bi.DictItemDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductConditionService {

    private static final String FUNNEL_CONFIG = "FUNNEL_CONFIG";

    @Autowired
    private BehaviourEventDetailStatDao behaviourEventDetailStatDao;
    @Autowired
    private DictItemDao dictItemDao;

    public List<EventConditionVO> eventCondition() {
        List<NameAttrBO> nameAttrBOS = behaviourEventDetailStatDao.eventCondition();
        List<EventConditionVO> eventConditionVOS = new ArrayList<>();
        nameAttrBOS.stream().collect(Collectors.toMap(NameAttrBO::getName, p -> {
                    List<String> list = new ArrayList<>();
                    list.add(p.getAttribute());
                    return list;
                },
                (List<String> l1, List<String> l2) -> {
                    l1.addAll(l2);
                    return l1;
                }
        )).forEach((k, v) -> {
            EventConditionVO eventConditionVO = new EventConditionVO();
            eventConditionVO.setEventName(k);
            eventConditionVO.setAttributes(v);
            eventConditionVOS.add(eventConditionVO);
        });
        eventConditionVOS.forEach(map -> {
            map.getAttributes().removeIf(att -> StringUtils.isBlank(att));
        });
        return eventConditionVOS;
    }

    public List<CodeNameVO> funnelCondition() {
        DictItemBO dictItemBO = new DictItemBO();
        dictItemBO.setDictCode(FUNNEL_CONFIG);
        List<DictItem> list = dictItemDao.findByParameter(dictItemBO);
        return list.stream().map(map -> {
            CodeNameVO codeNameVO = new CodeNameVO();
            codeNameVO.setCode(map.getCode());
            codeNameVO.setName(map.getVal());
            return codeNameVO;
        }).collect(Collectors.toList());
    }
}
